function validateFirstName(){
	var valid = true;
	var re =/^([^\s\!\@\#\$\%\^\&\*\(\)\-\_\=\+\]\}\[\{\'\"\;\:\?\.\>\,\<0-9]*)$/;
	if ( document.getElementById("firstName").value =="" )
    {
        document.getElementById("firstName").value=document.getElementById("hFirstName").value;
    }
	else if(!document.getElementById("firstName").value.match(re))
    {
    	document.getElementById("erFirstName").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Invalid expresion!";
        valid = false;
    }
	else
	{
		document.getElementById("erFirstName").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
	}
	return valid;
}

function validateLastName(){
	var valid = true;
	var re =/^([^\s\!\@\#\$\%\^\&\*\(\)\-\_\=\+\]\}\[\{\'\"\;\:\?\.\>\,\<0-9]*)$/;
    if ( document.getElementById("lastName").value =="" )
    {
        document.getElementById("lastName").value=document.getElementById("hLastName").value;
    }
    else if(!document.getElementById("lastName").value.match(re))
    {
    	document.getElementById("erLastName").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Invalid expresion!";
        valid = false;
    }
    else
    {
    	document.getElementById("erLastName").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function validateOldEmailAddress(){
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
	
    var valid = true;
    str = document.getElementById("oldEmailAddress").value;
    
    var mail = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if ( document.getElementById("oldEmailAddress").value == "" )
    {
    	document.getElementById("oldEmailAddress").value = document.getElementById("hEmailAddress").value;
    }
    else if (! document.getElementById("oldEmailAddress").value.match(mail))
    {
    	document.getElementById("erOldEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Email address not valid!";
	   	valid = false;
    }
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            if(xmlhttp.responseText.length == 265)
                {
                    //the user already exists in the DB
            		document.getElementById("erOldEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Email already in use!";
                    valid = false;
                }
        }
    };
    xmlhttp.open("GET","getUser.jsp?email="+str, true);
    xmlhttp.send();
    if(valid==true)
    {
    	document.getElementById("erOldEmailAddress").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function validateNewEmailAddress(){
	if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
	
    var valid = true;
    str = document.getElementById("newEmailAddress").value;
    var mail = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if ( document.getElementById("newEmailAddress").value == "" )
    {
    	document.getElementById("erNewEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Fill in your email!";
    	valid = false;
    }
    else if (! document.getElementById("newEmailAddress").value.match(mail))
    {
    	document.getElementById("erNewEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Email address not valid!";
	   	valid = false;
    }
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            if(xmlhttp.responseText.length == 265)
                {
                    //the user already exists in the DB
            		document.getElementById("erNewEmailAddress").innerHTML = "<img src=\"Images/error.png\" class=\"images\"/> Email already in use!";
                    valid = false;
                }
        }
    };
    xmlhttp.open("GET","getUser.jsp?email="+str, true);
    xmlhttp.send();
    if(valid==true)
    {
    	document.getElementById("erNewEmailAddress").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function validateOldPassword(){
	var valid=true;
	var oldPassword = document.getElementById("hPassword").toLocaleString();
	var typedOldPassword = document.getElementById("oldPassword").toLocaleString();
    if ( oldPassword ==  typedOldPassword )
    {
    	document.getElementById("erOldPassword").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    else
    {
    	document.getElementById("erOldPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\" />";
    	valid = false;
    }
    return valid;
}

function validateNewPassword(){
	var pas = /^([a-zA-Z0-9]*)$/;
    var valid = true;
    if ( !document.getElementById("newPassword").value.match(pas) )
    {
    	document.getElementById("erNewPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\" /> Invalid new password!";
        valid = false;
    }
    else if ( document.getElementById("newPassword").value == "" )
    {
    	document.getElementById("erNewPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\" /> Choose a password!";
        valid = false;
    }
    else if(document.getElementById("newPassword").value.length<6)
    {
    	document.getElementById("erNewPassword").innerHTML = "<img src=\"Images/error.png\" class=\"images\" /> Password too short(>6)!";
        valid = false;
    }
    else
    {
    	document.getElementById("erNewPassword").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function validateRetypedNewPassword(){
	valid = true;
    if(document.getElementById("newPassword").value!=document.getElementById("retypedNewPassword").value)
    {
    	document.getElementById("erRetypedNewPassword").innerHTML = "Don't match!";
    	valid =false;
    }
    else
    {
    	document.getElementById("erRetypedNewPassword").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function completeOldCity(){
    var valid = true;
    if ( 1==1 )
    {
        document.getElementById("oldCity").value=document.getElementById("hCity").value;
    }
    return valid;
}

function validateNewCity(){
	var city =/^([^\s\!\@\#\$\%\^\&\*\(\)\-\_\=\+\]\}\[\{\'\"\;\:\?\.\>\,\<0-9]*)$/;
    var valid = true;
    if ( !document.getElementById("newCity").value.match(city) )
    {
    	document.getElementById("erNewCity").innerHTML = "<img src=\"Images/error.png\" class=\"images\" /> City not the specified type!";
        valid = false;
    }
    else	 
    	if ( document.getElementById("newCity").value == "" )
    {
    	document.getElementById("erNewCity").innerHTML = "<img src=\"Images/error.png\" class=\"images\" /> Fill in the city field!";
        valid = false;
    }
    else{
    	document.getElementById("erNewCity").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function completeOldPhoneNumber(){
    if ( 1==1 )
    {
        document.getElementById("oldPhoneNumber").value=document.getElementById("hPhoneNumber").value;
    }
}

function validateNewPhoneNumber(){
	var phoneNumber=/^([^\d]*)$/;
    var valid = true;
    if(!document.getElementById("newPhoneNumber").value.match(phoneNumber))
    {
    	document.getElementById("erNewPhoneNumber").innerHTML = "<img src=\"Images/error.png\" class=\"images\" /> Phone nr. not the specified type!";
    }
    if ( document.getElementById("newPhoneNumber").value == "" )
    {
    	document.getElementById("erNewPhoneNumber").innerHTML = "<img src=\"Images/error.png\" class=\"images\" /> Fill in your phone number!";
        valid = false;
    }
    else{
    	document.getElementById("erNewPhoneNumber").innerHTML = "<img src=\"Images/ok.png\" class=\"images\" />";
    }
    return valid;
}

function validate(){
	var valid = true;
	var errorMesage = "<ul>Please fill in the following fields correctly before registering:";
	if (validateFirstName()==false)
	{	
		errorMesage = errorMesage + "<li>First Name</li>";
	}
	if (validateLastName()==false)
	{	
		errorMesage = errorMesage + "<li>Last Name</li>";
	}
	if (validateNewPhoneNumber()==false)
	{
		errorMesage = errorMesage + "<li>Phone Number</li>";
	}
	if (validateNewCity()==false)
	{
		errorMesage = errorMesage + "<li>City</li>";
	}
	if (validateOldEmailAddress()==false)
	{	
		errorMesage = errorMesage + "<li>Old Email Address</li>";
	}
	if (validateNewEmailAddress()==false)
	{	
		errorMesage = errorMesage + "<li>New Email Address</li>";
	}
	if (validateOldPassword()==false)
	{	
		errorMesage = errorMesage + "<li>Old Password</li>";
	}
	if (validateNewPassword()==false)
	{	
		errorMesage = errorMesage + "<li>New Password</li>";
	}
	if (validateRetypedNewPassword()==false)
	{	
		errorMesage = errorMesage + "<li>New Retyped Password</li></ul>";
	}
	if (validateFirstName()==false || validateLastName()==false || validatePhoneNumber()==false || validateNewCity()==false || validateOldEmailAddress()==false || validateNewEmailAddress()==false || validateOldPassword()==false || validateNewPassword()==false || validateRetypedNewPassword()==false)
	{	
		document.getElementById("errorMesageTab").style.display = "block";
		document.getElementById("errorMesageTab").innerHTML = errorMesage;
		valid = false;
	}
	else
	{
		valid = true;
	}
	return valid;
}

function validateName(){
	var valid = true;
	var errorMesage = "<ul>Please fill in the following fields correctly before submiting:";
	if (validateFirstName()==false)
	{	
		errorMesage = errorMesage + "<li>First Name</li>";
	}
	if (validateLastName()==false)
	{	
		errorMesage = errorMesage + "<li>Last Name</li></ul>";
	}
	if (validateFirstName()==false || validateLastName()==false )
	{	
		document.getElementById("nameUpdateError").style.display = "block";
		document.getElementById("nameUpdateError").innerHTML = errorMesage;
		valid = false;
	}
	else
	{
		valid = true;
	}
	return valid;
}

function validateEmail(){
	var valid = true;
	var errorMesage = "<ul>Please fill in the following fields correctly before submiting:";
	if (validateOldEmailAddress()==false)
	{	
		errorMesage = errorMesage + "<li>Old Email Address</li>";
	}
	if (validateNewEmailAddress()==false)
	{	
		errorMesage = errorMesage + "<li>New Email Address</li></ul>";
	}
	if (validateOldEmailAddress()==false || validateNewEmailAddress()==false )
	{	
		document.getElementById("emailUpdateError").style.display = "block";
		document.getElementById("emailUpdateError").innerHTML = errorMesage;
		valid = false;
	}
	else
	{
		valid = true;
	}
	return valid;
}

function validatePassword(){
	var valid = true;
	var errorMesage = "<ul>Please fill in the following fields correctly before submiting:";
	if (validateOldPassword()==false)
	{	
		errorMesage = errorMesage + "<li>Old Password</li>";
	}
	if (validateNewPassword()==false)
	{	
		errorMesage = errorMesage + "<li>New Password</li>";
	}
	if (validateRetypedNewPassword()==false)
	{	
		errorMesage = errorMesage + "<li>New Retyped Password</li></ul>";
	}
	if (validateOldPassword()==false || validateNewPassword()==false || validateRetypedNewPassword()==false)
	{	
		document.getElementById("passwordUpdateError").style.display = "block";
		document.getElementById("passwordUpdateError").innerHTML = errorMesage;
		valid = false;
	}
	else
	{
		valid = true;
	}
	return valid;
}

function validatePhoneNumber(){
	var valid = true;
	var errorMesage = "<ul>Please fill in the following fields correctly before submiting:";
	if (validateNewPhoneNumber()==false)
	{
		errorMesage = errorMesage + "<li>New Phone Number</li></ul>";
	}
	if (validateNewPhoneNumber()==false)
	{	
		document.getElementById("phoneNumberUpdateError").style.display = "block";
		document.getElementById("phoneNumberUpdateError").innerHTML = errorMesage;
		valid = false;
	}
	else
	{
		valid = true;
	}
	return valid;
}

function validateCity(){
	var valid = true;
	var errorMesage = "<ul>Please fill in the following fields correctly before submiting:";
	if (validateNewCity()==false)
	{
		errorMesage = errorMesage + "<li>New City</li></ul>";
	}
	if (validateNewPhoneNumber()==false)
	{	
		document.getElementById("cityUpdateError").style.display = "block";
		document.getElementById("cityUpdateError").innerHTML = errorMesage;
		valid = false;
	}
	else
	{
		valid = true;
	}
	return valid;
}

function toggleName() {
	if(document.getElementById("secondDiv1").style.display == "block") {
		document.getElementById("secondDiv1").style.display = "none";
		document.getElementById("displayText1").innerHTML = "Change";
		document.getElementById("firstDiv1").style.display = "block";
  	}
	else {
		document.getElementById("secondDiv1").style.display = "block";
		document.getElementById("displayText1").innerHTML = "Hide";
		document.getElementById("firstDiv1").style.display = "none";
	}
}
function toggleEmail() {
	if(document.getElementById("secondDiv2").style.display == "block") {
		document.getElementById("secondDiv2").style.display = "none";
		document.getElementById("displayText2").innerHTML = "Change";
		document.getElementById("firstDiv2").style.display = "block";
  	}
	else {
		document.getElementById("secondDiv2").style.display = "block";
		document.getElementById("displayText2").innerHTML = "Hide";
		document.getElementById("firstDiv2").style.display = "none";
	}
}
function togglePassword() {
	if(document.getElementById("secondDiv3").style.display == "block") {
		document.getElementById("secondDiv3").style.display = "none";
		document.getElementById("displayText3").innerHTML = "Change";
		document.getElementById("firstDiv3").style.display = "block";
  	}
	else {
		document.getElementById("secondDiv3").style.display = "block";
		document.getElementById("displayText3").innerHTML = "Hide";
		document.getElementById("firstDiv3").style.display = "none";
	}
}
function toggleCity() {
	if(document.getElementById("secondDiv4").style.display == "block") {
		document.getElementById("secondDiv4").style.display = "none";
		document.getElementById("displayText4").innerHTML = "Change";
		document.getElementById("firstDiv4").style.display = "block";
  	}
	else {
		document.getElementById("secondDiv4").style.display = "block";
		document.getElementById("displayText4").innerHTML = "Hide";
		document.getElementById("firstDiv4").style.display = "none";
	}
}
function togglePhoneNumber() {
	if(document.getElementById("secondDiv5").style.display == "block") {
		document.getElementById("secondDiv5").style.display = "none";
		document.getElementById("displayText5").innerHTML = "Change";
		document.getElementById("firstDiv5").style.display = "block";
  	}
	else {
		document.getElementById("secondDiv5").style.display = "block";
		document.getElementById("displayText5").innerHTML = "Hide";
		document.getElementById("firstDiv5").style.display = "none";
	}
}